package com.test.auth_db_auto.config;

import com.test.auth_db_auto.support.BootSecurityProperties;
import com.test.auth_db_auto.support.code.BootCodeService;
import com.test.auth_db_auto.support.code.RedisCodeService;
import com.test.auth_db_auto.support.code.SessionCodeService;
import com.test.auth_db_auto.support.properities.CodeStoreType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ Author: .Mr
 * @ ClassName WebConfig
 * @ Description TODO
 * @ date 2021/12/13 13:49
 * @ Version 1.0
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**")
                .addResourceLocations("classpath:/statics/");

        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/statics/favicon.ico");
    }

   /* @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/base-login").setViewName("base-login");
    }*/

    /**
     * 设置code存储服务的对象
     * */
    @Bean
    public BootCodeService codeService(StringRedisTemplate template, BootSecurityProperties properties){
        if (properties.getCodeStoreType() == CodeStoreType.redis) {
            return  new RedisCodeService(properties.getCodeExpireTime());
        }else {
            return  new SessionCodeService();
        }
    }
}
